package personalPage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import fourFragment.adapter.MyQuestionAdapter;
import fourFragment.bean.MyQuestionListBean;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/4/6.
 * 别人发表的问题
 */

@ContentView(R.layout.my_question)
public class QuestionList extends BaseActivity implements View.OnClickListener {
    private int page;
    private static final int REFRESH = 0;
    private static final int LOAD = 1;
    private static final int ZAN = 2;
    private String userid;
    private String from;

    @ViewInject(R.id.common_back)
    private ImageView back;
    @ViewInject(R.id.common_title)
    private TextView title;
    @ViewInject(R.id.my_question_refresh_loadLayout)
    private MyPtrRefresh_LoadView myRefresh_loadLayout;
    @ViewInject(R.id.my_question_listview)
    private ListView listView;
    private MyQuestionAdapter myQuestionAdapter;
    private ArrayList<MyQuestionListBean.QuestionListBean> arrayList;
    private MyHandler myHandler;

    private static class MyHandler extends Handler{
        WeakReference<QuestionList>weakReference;

        public MyHandler(QuestionList weakReference) {
            this.weakReference = new WeakReference<>(weakReference);
        }

        @Override
        public void handleMessage(Message msg) {
            QuestionList questionActivity = weakReference.get();
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    if(msg.arg1!=ZAN) {
                        Gson gson = new Gson();
                        MyQuestionListBean myQuestionListBean = gson.fromJson((String) msg.obj, MyQuestionListBean.class);
                        if (myQuestionListBean != null) {
                            List<MyQuestionListBean.QuestionListBean> questionList = myQuestionListBean.getQuestionList();
                            if (questionList != null) {
                                if (questionList.size() > 0) {
                                    switch (msg.arg1) {
                                        case REFRESH:
                                            questionActivity.arrayList.clear();
                                            break;
                                        default:
                                            break;
                                    }
                                    for (int i = 0; i < questionList.size(); i++) {
                                        questionActivity.arrayList.add(questionList.get(i));
                                    }
                                } else {
                                    if (msg.arg1 == LOAD) {
                                        questionActivity.page--;
                                    }
                                }
                                questionActivity.myQuestionAdapter.notifyDataSetChanged();
                                questionActivity.myRefresh_loadLayout.OnRefreshComplete();
                            }
                        }
                    }else{
                        try {
                            JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                            String err_code = jsonObject.getString("err_code");
                            if("0".equals(err_code)){
                                //赞
                                String praise = questionActivity.arrayList.get(msg.arg2).getPraise();
                                int i = Integer.parseInt(praise) + 1;
                                questionActivity.arrayList.get(msg.arg2).setPraise(String.valueOf(i));
                                questionActivity.arrayList.get(msg.arg2).setIs_prised(1);
                                questionActivity.myQuestionAdapter.notifyDataSetChanged();
                            }else{
                                //取消赞
                                String praise = questionActivity.arrayList.get(msg.arg2).getPraise();
                                int i = Integer.parseInt(praise) - 1;
                                questionActivity.arrayList.get(msg.arg2).setPraise(String.valueOf(i));
                                questionActivity.arrayList.get(msg.arg2).setIs_prised(0);
                                questionActivity.myQuestionAdapter.notifyDataSetChanged();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userid = getIntent().getStringExtra("userId");
        from = getIntent().getStringExtra("from");
        myHandler = new MyHandler(this);
        initView();
        getDataFromInterNet(page = 1, REFRESH,false);
    }

    private void initView() {
        myRefresh_loadLayout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, LOAD,false);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, REFRESH,false);
            }
        });
        arrayList = new ArrayList<>();
        myQuestionAdapter = new MyQuestionAdapter(this,arrayList);
        listView.setAdapter(myQuestionAdapter);
        title.setText("发表问题");
        back.setOnClickListener(this);
    }

    private void getDataFromInterNet(int page, int flag,boolean waitDialog) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("userid", userid);
        hashMap.put("type", "question");
        hashMap.put("from", from);
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.PERSONALDETAIL, myHandler, hashMap, flag,waitDialog);
    }

    private void praise(int position){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("dialog_id", arrayList.get(position).getDialogid());
        new InterNetController(this, Constant.DIALOGPRISE, myHandler, hashMap, ZAN, position, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                QuestionList.this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                //查看免费问题
                int zan1 = data.getIntExtra("zan", 0);
                int comment1 = data.getIntExtra("comment", 0);
                boolean isPraised1 = data.getBooleanExtra("isPraised",false);
                arrayList.get(requestCode).setComment(String.valueOf(comment1));
                arrayList.get(requestCode).setPraise(String.valueOf(zan1));
                if(isPraised1) {
                    arrayList.get(requestCode).setIs_prised(1);
                }else{
                    arrayList.get(requestCode).setIs_prised(0);
                }
                myQuestionAdapter.notifyDataSetChanged();
                break;
            case 2:
                //查看付费问题
                int zan = data.getIntExtra("zan", 0);//赞的数量
                boolean comment = data.getBooleanExtra("comment", false);//判断是否已经评价
                boolean isPraised = data.getBooleanExtra("isPraised",false);
                String state = arrayList.get(requestCode).getState();
                if(isPraised) {
                    arrayList.get(requestCode).setIs_prised(1);
                }else{
                    arrayList.get(requestCode).setIs_prised(0);
                }
                //修改状态
                switch (state) {
                    case "未查看":
                        if (comment) {
                            arrayList.get(requestCode).setState("正常");
                        } else {
                            arrayList.get(requestCode).setState("未评价");
                        }
                        break;
                    case "未评价":
                        if (comment) {
                            arrayList.get(requestCode).setState("正常");
                        }
                        break;
                    default:
                        break;
                }
                //修改赞
                arrayList.get(requestCode).setPraise(String.valueOf(zan));
                myQuestionAdapter.notifyDataSetChanged();
                break;
        }
    }
}
